package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.SmsHomeAdvertise;
import cn.zcscloud.mall.service.SmsHomeAdvertieService;
import cn.zcscloud.mall.service.impl.SmsHomeAdvertieServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/home/advertise")
public class SmsHomeAdvertiesController {
    @Autowired
    private SmsHomeAdvertieService smsHomeAdvertieService;

    /**
     * 条件分页展示广告
     * @param name 名称模糊匹配
     * @param type 类型
     * @param endTime 到期时间
     * @param pageSize 显示记录数
     * @param pageNum 当前页
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(value = "name", required = false) String name,
                  @RequestParam(value = "type", required = false) Integer type,
                  @RequestParam(value = "endTime", required = false) String endTime,
                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        SmsHomeAdvertise homeAdvertie = new SmsHomeAdvertise();
        QueryWrapper<SmsHomeAdvertise> lqw = new QueryWrapper<>();
        lqw.eq(endTime != null,"date_format(end_time,'%Y-%m-%d')",endTime)
                .lambda().like(name != null,SmsHomeAdvertise::getName,name)
                .eq(type != null,SmsHomeAdvertise::getType,type);
        Page<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        Page<SmsHomeAdvertise> iPage = smsHomeAdvertieService.page(page, lqw);
        return R.success(PageRequest.init(iPage));
    }

    /**
     * 获取广告详情
     * @param id 主键
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertieService.getById(id);
        return R.success(smsHomeAdvertise);
    }

    /**
     * 添加广告
     * @param smsHomeAdvertise
     * @return
     */
    @PostMapping("/create")
    public R save(@RequestBody SmsHomeAdvertise smsHomeAdvertise){
        LambdaQueryWrapper<SmsHomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(smsHomeAdvertise.getName() != null,SmsHomeAdvertise::getName,smsHomeAdvertise.getName());
        if (smsHomeAdvertise.getStartTime().compareTo(smsHomeAdvertise.getEndTime())>0){
            return R.error("结束时间不得早于开启时间");
        }
        List<SmsHomeAdvertise> list = smsHomeAdvertieService.list(queryWrapper);
        if (list == null || list.isEmpty()){
            boolean save = smsHomeAdvertieService.save(smsHomeAdvertise);
            return R.success("");
        }
        return R.error("name属性重复");
    }

    /**
     * 删除广告
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestParam("ids") List<Long> ids){
        smsHomeAdvertieService.removeByIds(ids);
        return R.success("");
    }

    /**
     * 修改上下线状态
     * @param id
     * @param status 上下线状态
     * @return
     */
    @PostMapping("/update/status/{id}")
    public R updateStatus(@PathVariable Long id, Integer status){
        SmsHomeAdvertise smsHomeAdvertise = new SmsHomeAdvertise();
        smsHomeAdvertise.setId(id);
        smsHomeAdvertise.setStatus(status);
        boolean b = smsHomeAdvertieService.updateById(smsHomeAdvertise);
        if (b){
            return R.success("");
        }
        return R.error();
    }

    /**
     * 修改广告
     * @param id
     * @param advertise
     * @return
     */
    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @RequestBody SmsHomeAdvertise advertise){
        LambdaQueryWrapper<SmsHomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsHomeAdvertise::getName,advertise.getName())
                        .ne(SmsHomeAdvertise::getId,advertise.getId());
        if (!smsHomeAdvertieService.list(queryWrapper).isEmpty()) {
            return R.error("name属性重复");
        }

        smsHomeAdvertieService.updateById(advertise);
        return R.success("");
    }

}
